package panel.viewSpots.navigation
{
	import assists.HtmlText;
	import assists.Lang;
	import assists.TextFieldUtils;
	import assists.URI;
	import assists.media.SoundMedia;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import inerface.INavigationDetail;
	import inerface.IPanel;
	
	import panel.stars.StarGroup;
	
	public class NavigationDetailPanel extends Sprite implements INavigationDetail
	{
		private var _nameTxt:TextField = new TextField();
		private var _commitNum:TextField = new TextField();
		private var _openTimeTitle:TextField = new TextField();
		private var _openTime:TextField = new TextField();
		private var _descTitle:TextField = new TextField();
		private var _desc:TextField = new TextField();
		private var _soundTitle:TextField = new TextField();
		
		private var _appraiseBtn:BtnClouree;
		private var _visitBtn:Sprite = new Sprite();
		private var _starGroup:StarGroup;
		private var _image:Bitmap;
		
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		private var _uri:URI;
		private var _lang:Lang;
		private var _descContainer:Sprite = new Sprite();
		private var _descDragContainer:Sprite = new Sprite();
		private var _visitedBtn:SpotVisitedBtn;
		private var _onOpenCommitView:Function;
		private var _line1:Bitmap;
		private var _line2:Bitmap;
		private var _soundModule:SoundModule;
		private var _onOpenSound:Function;
		private var _data:Object;
		public function NavigationDetailPanel()
		{
			super();
		}
		
		public function init(stage:Stage, loader:ResourceLoader,lang:Lang,uri:URI=null):void
		{
			_stage = stage;
			_loader = loader;
			_uri = uri;
			_lang = lang;
			_line1 = _loader.uiManager.generateBmp(_loader.uiManager.cutLine,true);
			_line2 = _loader.uiManager.generateBmp(_loader.uiManager.cutLine,true);
			layout();
		}
		
		private function layout():void
		{
			_nameTxt.mouseEnabled = false;
			_nameTxt.autoSize = "left";
			
			_commitNum.mouseEnabled = false;
			_commitNum.autoSize = "left";
			
			
			_soundTitle.mouseEnabled = false;
			_soundTitle.autoSize = "left";
			TextFieldUtils.setHtmlText(_soundTitle,HtmlText.formatSizeColor("语音导游",0xffffff,24));
			
			_soundModule = new SoundModule(_stage,_loader,null);
			
			
			_openTimeTitle.mouseEnabled = false;
			_openTimeTitle.autoSize = "left";
			TextFieldUtils.setHtmlText(_openTimeTitle,HtmlText.formatSizeColor("开放时间",0xffffff,24));
			
			_openTime.mouseEnabled = false;
			_openTime.autoSize = "left";
			
			_descTitle.mouseEnabled = false;
			_descTitle.autoSize = "left";
			TextFieldUtils.setHtmlText(_descTitle,HtmlText.formatSizeColor("描述",0xffffff,24));
			
			_desc.mouseEnabled = false;
			_desc.width = _stage.stageWidth * 0.9;
			_desc.autoSize = "left";
			_desc.wordWrap = true;
			_desc.multiline = true;
			
			_appraiseBtn = new BtnClouree();
			_appraiseBtn.label("评价");
			_appraiseBtn.addEventListener(MouseEvent.CLICK,clickAppraiseHandler);
			
			_visitedBtn = new SpotVisitedBtn(_loader,_stage);
			_line1.width = _stage.stageWidth * 0.9;
			_line1.x = _stage.stageWidth * 0.05;
			
			_line2.width = _stage.stageWidth * 0.9;
			_line2.x = _stage.stageWidth * 0.05;
			
			this.addChild(_soundTitle);
			this.addChild(_soundModule);
			this.addChild(_nameTxt);
			this.addChild(_commitNum);
			this.addChild(_openTimeTitle);
			this.addChild(_openTime);
			this.addChild(_descTitle);
			this.addChild(_appraiseBtn);
			_descDragContainer.addChild(_desc);
			_descContainer.addChild(_descDragContainer);
			this.addChild(_descContainer);
			this.addChild(_visitedBtn);
			this.addChild(_line1);
			this.addChild(_line2);
		}
		
		public function set onOpenCommitView(value:Function):void
		{
			_onOpenCommitView = value;
		}
		
		protected function clickAppraiseHandler(event:MouseEvent):void
		{
			_onOpenCommitView();
		}
		
		private function resize():void
		{
			var distance:Number = _stage.stageWidth * 0.05;
			_image.x = distance;
			_image.y = 20;
			
			_nameTxt.x = _image.x + _image.width + distance;
			_nameTxt.y = _image.y;
			
			_starGroup.x = _nameTxt.x;
			_starGroup.y = _nameTxt.y + _nameTxt.height;
			
			_commitNum.x = _nameTxt.x;
			_commitNum.y = _starGroup.y + _starGroup.height;
			
			_soundTitle.x = _image.x;
			_soundTitle.y = _image.y + _image.height + distance;
			
			_soundModule.x = 0;
			_soundModule.y = _soundTitle.y + _soundTitle.height + _soundModule.height * 0.5;
			
			_line2.y = _soundModule.y + _soundModule.height + _line2.height - _soundModule.height * 0.5;
			
			_openTimeTitle.x = _image.x;
			_openTimeTitle.y = _line2.y + _line2.height +  _line2.height;
			
			_openTime.x = _openTimeTitle.x;
			_openTime.y = _openTimeTitle.y + _openTimeTitle.height;
			
			_line1.y = _openTime.y + _openTime.height + _line1.height;
			
			_descTitle.x = _openTimeTitle.x;
			_descTitle.y = _openTime.y + _openTime.height + 2  * _line1.height;
			
			_descContainer.x = _openTime.x;
			_descContainer.y = _descTitle.y + _descTitle.height;
			
			_appraiseBtn.x = _stage.stageWidth - _appraiseBtn.width - distance;
			_appraiseBtn.y = _commitNum.y;
 			
			_visitedBtn.y = _stage.stageHeight - _visitedBtn.height;
			
			if(_desc.height > _stage.stageHeight - _descContainer.y - _visitedBtn.height)
			{
			scrollDescTxt(_desc.width,_stage.stageHeight - _descContainer.y - _visitedBtn.height - 20);
			_descContainer.addEventListener(MouseEvent.MOUSE_DOWN,downhandler);
			}
			
			this.graphics.clear();
			this.graphics.lineStyle(2,0xffffff);
			this.graphics.moveTo(0,_image.y +　_image.height + distance / 2);
			this.graphics.lineTo(_stage.stageWidth,_image.y +　_image.height + distance / 2);
		}
		
		protected function downhandler(event:MouseEvent):void
		{
			_descDragContainer.startDrag(false,new Rectangle(0,0,0,-(_stage.stageHeight - _descContainer.y - _visitedBtn.height)));
			_stage.addEventListener(MouseEvent.MOUSE_UP,upHandler);
		}
		
		protected function upHandler(event:MouseEvent):void
		{
			_descDragContainer.stopDrag();
			_stage.removeEventListener(MouseEvent.MOUSE_UP,upHandler);
		}
		
		private function scrollDescTxt(wid:Number,hei:Number):void
		{
			var mask:Shape = new Shape();
			mask.graphics.beginFill(0);
			mask.graphics.drawRect(0,0,wid,hei);
			mask.graphics.endFill();
			_descContainer.addChild(mask);
			_descDragContainer.mask = mask;
		}
		
		public function show(data:Object,icon:Bitmap):void
		{
			_data = data;
			_image = icon;
			_image.width = _image.height = _stage.stageWidth * 0.25;
			this.addChild(_image);
			TextFieldUtils.setHtmlText(_nameTxt,HtmlText.formatSizeColor(data.name,0xffffff,26));
			TextFieldUtils.setHtmlText(_commitNum,HtmlText.formatSizeColor("618人评价",0xffffff));
			if(data.openStartTime == "")
			{
			TextFieldUtils.setHtmlText(_openTime,HtmlText.formatSizeColor("全天开放",0xffffff));
			}else
			{
				var startList:Array = data.openStartTime.split(",");
				var endList:Array = data.openEndTime.split(",");
				var openStr:String = "";
				for(var i:int=0;i<startList.length;i++)
				{
				openStr = openStr + startList[i] + "----" + endList[i] + "\n";
				}
				TextFieldUtils.setHtmlText(_openTime,HtmlText.formatSizeColor(openStr,0xffffff));
			}
			TextFieldUtils.setHtmlText(_desc,HtmlText.formatSizeColor(_lang.client(data.desc),0xffffff));
			_starGroup = new StarGroup();
			_starGroup.starLevel = data.star;
			this.addChild(_starGroup);
			
			resize();
		}
		
		public function set onSoundMedia(value:SoundMedia):void
		{
			_soundModule.onSoundMedia = value;
		}
		
		public function set onOpenSound(value:Function):void
		{
			_onOpenSound = value;
			_soundModule.onOpenNewSound = function():void
			{
				if(_data.mp3 == "") return;
				_onOpenSound(_data.mp3);
			}
		}
		
		public function clear():void
		{
		
		}
	}
}